package cn.hj.product.service.impl;

import cn.hj.basic.service.impl.BaseServiceImpl;
import cn.hj.product.domain.Product;
import cn.hj.product.domain.ProductDetail;
import cn.hj.product.mapper.ProductDetailMapper;
import cn.hj.product.mapper.ProductMapper;
import cn.hj.product.service.IProductService;
import cn.hj.user.domain.Logininfo;
import cn.hj.user.domain.User;
import cn.hj.user.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class ProductServiceImpl extends BaseServiceImpl<Product> implements IProductService {

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private ProductDetailMapper productDetailMapper;

    @Autowired
    private UserMapper userMapper;


    /**
     * 保存product数据和ProductDetail数据
     * @param product
     */
    @Override
    @Transactional
    public void add(Product product) {
        //保存product数据返回id
        productMapper.insert(product);

        ProductDetail detail = product.getDetail();
        detail.setProduct_id(product.getId());
        //保存product数据返回idProductDetail数据
        productDetailMapper.insert(detail);
    }


    /**
     * 上架服务
     * @param ids
     * @return
     */
    @Override
    @Transactional
    public void onSale(List<Long> ids) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("onsaletime",new Date());
        map.put("ids",ids);
        productMapper.onSale(map);
    }

    /**
     * 下架服务
     * @param ids
     * @return
     */
    @Override
    @Transactional
    public void offSale(List<Long> ids) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("offsaleTime",new Date());
        map.put("ids",ids);
        productMapper.offSale(map);
    }

    /**
     * 新增·服务
     * @param product
     * @param currentLogininfo
     */
    @Override
    @Transactional
    public void save(Product product) {
        product.setCreatetime(new Date());
        productMapper.insert(product);
        System.out.println(product.getId());
        ProductDetail detail = product.getDetail();
        detail.setProduct_id(product.getId());
        productDetailMapper.insert(detail);

    }


    /**
     * 查询服务信息加详情
     * @param id
     * @return
     */
    @Override
    public Product findById(Long id) {
        return productMapper.loadByIdDetail(id);
    }

    /**
     * 购买服务
     * @param id
     */
    @Override
    @Transactional
    public void adopt(Long id, Logininfo logininfo) {

    }
}
